package com.fzd.code.captcha.service;

import com.fzd.code.captcha.service.i.IRedisCaptchaService;
import com.fzd.code.captcha.service.i.ISessionCaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service("session-mycatchap-service")
public class SessionMyCaptchaService implements ISessionCaptchaService {
    @Autowired
    MyCaptchaUtil myCaptchaUtil;

    @Override
    public void create(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        myCaptchaUtil.getRandcode(request, response);//输出图片方法
    }

    @Override
    public boolean validate(HttpServletRequest request, String code) {
        if(request.getSession().getAttribute(MyCaptchaUtil.RANDOMCODEKEY)!=null){
            if(request.getSession().getAttribute(MyCaptchaUtil.RANDOMCODEKEY).toString().toLowerCase().equals(code.toLowerCase())){
                return true;
            }
        }
        return false;
    }
}
